package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.MyToast;
import com.example.permission.myjbm.activitys.Release_Commodity_1;
import com.example.permission.myjbm.dao.BaseAty;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 * Created by 家帮忙 on 2017/1/12.
 */

/**
 * 发布
 */
public class IssoHomeActivity extends BaseAty {
    @InjectView(R.id.ealease_recycler)
    RecyclerView ealeaseRecycler;
    @InjectView(R.id.aty_release)
    LinearLayout atyRelease;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_release);
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        ealeaseRecycler.setLayoutManager(new GridLayoutManager(this, 2));
        ealeaseRecycler.setAdapter(new MyAdapter());
    }

    class MyAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyHolder(View.inflate(IssoHomeActivity.this, R.layout.release_recycler_item, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            String str = "title";
            switch (position) {
                case 0:
                    str = "商品广告";
                    break;
                case 1:
                    str = "公共信息";
                    break;
                case 2:
                    str = "互助信息";
                    break;
                case 3:
                    str = "趣味八卦";
                    break;
            }
            ((MyHolder) holder).tv.setText(str);
            ((MyHolder) holder).ln.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(IssoHomeActivity.this, "执行了点击" + position, Toast.LENGTH_SHORT).show();
                    switch (position) {
                        case 0:
                            Intent intent = new Intent(IssoHomeActivity.this, Release_Commodity_1.class);
                            startActivity(intent);
                            break;
                        case 1:
                            break;
                    }


                }
            });

        }

        @Override
        public int getItemCount() {
            return 4;
        }

        class MyHolder extends RecyclerView.ViewHolder {

            private final TextView tv;
            private final LinearLayout ln;

            public MyHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.release_recycler_tv);
                ln = (LinearLayout) itemView.findViewById(R.id.release_recycler_ln);


            }
        }

    }

    //检测退出时间
    private long curren = 0;
    @Override
    public void onBackPressed() {

        if ((System.currentTimeMillis() - curren) > 2000) {
            MyToast.show(IssoHomeActivity.this, "再按一次退出", 0);
            curren = System.currentTimeMillis();


        } else {
            getSharedPreferences("open_key_judge", Activity.MODE_PRIVATE).edit().clear().commit();
            ExitAppUtlis.getInstance().exit();

        }


    }
}
